package com.heima.wemedia.Aliyunutil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.imageaudit20191230.models.ScanTextRequest;
import com.aliyun.imageaudit20191230.models.ScanTextResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.teautil.Common;

public class textcheck {
    public static com.aliyun.imageaudit20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        /*
          初始化配置对象com.aliyun.teaopenapi.models.Config
          Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
         */
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
        return new com.aliyun.imageaudit20191230.Client(config);
    }

    public static String theone (String content) throws Exception {
        // 创建AccessKey ID和AccessKey Secret，请参考https://help.aliyun.com/document_detail/175144.html。
        // 如果您使用的是RAM用户的AccessKey，还需要为RAM用户授予权限AliyunVIAPIFullAccess，请参考https://help.aliyun.com/document_detail/145025.html。
        // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
        String accessKeyId = "LTAI5tDJDzuzL3hCW8H6mQWd";
        String accessKeySecret = "UQ9PfoW2nLVBESJu3RH01Fwb4EcGvV";
        com.aliyun.imageaudit20191230.Client client = textcheck.createClient(accessKeyId, accessKeySecret);
        ScanTextRequest.ScanTextRequestTasks tasks = new ScanTextRequest.ScanTextRequestTasks()
                .setContent(content);
        ScanTextRequest.ScanTextRequestLabels labels = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("ad");
        ScanTextRequest.ScanTextRequestLabels labels1 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("spam");
        ScanTextRequest.ScanTextRequestLabels labels2 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("politics");
        ScanTextRequest.ScanTextRequestLabels labels3 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("abuse");
        ScanTextRequest.ScanTextRequestLabels labels4 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("terrorism");
        ScanTextRequest.ScanTextRequestLabels labels5 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("porn");
        ScanTextRequest.ScanTextRequestLabels labels6 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("flood");
        ScanTextRequest.ScanTextRequestLabels labels7 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("contraband");
        com.aliyun.imageaudit20191230.models.ScanTextRequest scanTextRequest = new com.aliyun.imageaudit20191230.models.ScanTextRequest()
                .setLabels(java.util.Arrays.asList(
                        labels,
                        labels1,
                        labels2,
                        labels3,
                        labels4,
                        labels5,
                        labels6,
                        labels7
                ))
                .setTasks(java.util.Arrays.asList(
                        tasks
                ));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印API的返回值
            ScanTextResponse response = client.scanTextWithOptions(scanTextRequest, runtime);
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response.body.getData())));
            String s = Common.toJSONString(TeaModel.buildMap(response.body.getData()));
            System.out.println(s);
            JSONObject jsonObject = JSON.parseObject(s);
            Object elements = jsonObject.get("Elements");
            String s1 = elements.toString();
            JSONArray objects = JSONArray.parseArray(s1);
            JSONObject jsonObject1 = objects.getJSONObject(0);
            JSONArray results = jsonObject1.getJSONArray("Results");
            JSONObject resultObj = results.getJSONObject(0);
            String suggestion = resultObj.getString("Suggestion");
            System.out.println(suggestion);
            return suggestion;
        } catch (TeaException error) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(error));
            // 获取单个字段
            System.out.println(error.getCode());
            return "";
        }
    }
}
